package de.hdm.articastle;

import java.util.List;

import android.graphics.Color;

import de.hdm.articastle.framework.Game;
import de.hdm.articastle.framework.Graphics;
import de.hdm.articastle.framework.Input.TouchEvent;
import de.hdm.articastle.framework.Screen;

public class MainMenuScreen extends Screen {
	
	public MainMenuScreen(Game game) {
		super(game);
	}

	@Override
	public void update(float deltaTime) {
		Graphics g = game.getGraphics();
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		
		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (inBounds(event, 0, g.getHeight() - 36, 36, 36)) {
					Settings.soundEnabled = !Settings.soundEnabled;					
				}
				if (inBounds(event, 128, 120, 64, 64)) {
					return;
				}
				if (inBounds(event, 128, 120, 128, 64)) {
					game.setScreen(new GameScreen(game));
					return;
				}
				if (inBounds(event, 128, 120, 192, 64)) {
					return;
				}
			}
		}
	}
	
	private boolean inBounds(TouchEvent event, int x, int y, int width, 
			int height) {
		if (event.x > x && event.x < x + width - 1 &&
				event.y > y && event.y < y + height - 1)
			return true;
		else
			return false;
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		
		g.clear(Color.WHITE);
		g.drawPixmap(Assets.logo, 120, 20, 0, 123, 0, 115);
		g.drawPixmap(Assets.buttons, 128, 120);
		if (Settings.soundEnabled)
			g.drawPixmap(Assets.soundEnabled, 0, 384);
		else
			g.drawPixmap(Assets.soundDisabled, 0, 384);
	}

	@Override
	public void pause() {
		Settings.save(game.getFileIO());		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
